<?php

namespace zhtfrontend\controllers;

use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use common\models\datas\CardBatch;
use common\models\datas\Site;
use doumiaoduo\buildform\BuildForm;
use yii\data\Pagination;
use yii\helpers\Json;
use yii\web\Response;
use yii\web\BadRequestHttpException;
use common\components\actions\DeleteAction;

class BatchController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = CardBatch::find()->where(['site_id' => Fun::getSiteId()])->orderBy('apply_time DESC');
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['mobile'])) {
                $data->andWhere(['like', 'phone', $search_data['mobile']]);
            }
            if (!empty($search_data['title'])) {
                $data->andWhere(['like', 'title', $search_data['title']]);
            }
        }
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['zhtfrontend_operate', 'admin_name']);
        return $this->render('index', ['data' => $array, 'pages' => $pages]);
    }


    public function actionCreate()
    {
        $this->layout = false;
        $model = new CardBatch();
        $model->site_id = Fun::getSiteId();
        $site = Site::findOne(['id' => $model->site_id]);
        $model->city_id = $site != null ? $site->web_site_id : '';
        $model->site_user_id = \Yii::$app->user->id;
        $model->sendtype = 0;

        $form = new BuildForm('@app/components/forms/batchForm.php');
        $form->loadModel(['batch' => $model]);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {

            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['batch/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actionUpdate()
    {
        $this->layout = false;
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model = CardBatch::findOne(\Yii::$app->request->get('id'));
        if ($model === null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $form = new BuildForm('@app/components/forms/batchForm.php');
        $form->loadModel(['batch' => $model]);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model->site_user_id = \Yii::$app->user->id;
            $model->status = 0;
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '信息修改成功'), Fun::url(['batch/index']));
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actions()
    {
        return [
            'delete' => [
                'class' => DeleteAction::className(),
                'modelClass' => CardBatch::className(),
                'message' => ['success' => \Yii::t('zhtfrontend', '删除成功')],
                'redirectTo' => 'batch/index',
            ],
        ];
    }


}
